// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
var item2_param = {};
var globalEnum = require("./globalEnum");

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:
    getAction: function() {
        return {
            rotateAction: {
                action: cc.repeatForever(cc.rotateBy(3, 360)),
                flag: false
            },
            moveAction: {
                action: cc.moveTo(1.5, cc.v2(-200, -200)),
                flag: false
            }
        };
    },

    moveAction: function() {
        let moveAction = item2_param.moveAction.action;
        if (!item2_param.moveAction.flag) {
            this.node.runAction(moveAction);
            item2_param.moveAction.flag = true;
        } else {
            item2_param.moveAction.flag = false;
            this.node.stopAction(item2_param.moveAction.action);
        }
    },

    onLoad () {
        // 動作初始化
        item2_param = this.getAction();

        // 自定義事件監聽
        this.node.on(globalEnum.event.item2Move, this.moveAction, this);
        
        // 操作事件放在最後
        this.node.on(cc.Node.EventType.TOUCH_START, function(t) {
            console.log("觸摸了一下：", t);
            this.node.emit(globalEnum.event.item2Move);
            let item1 = cc.find(globalEnum.nodePath.item1);
            item1.emit(globalEnum.event.item1Move);
        }, this);
    },

    start () {

    },

    // update (dt) {},
});
